package cn.csdj.sism.web;

import cn.csdj.sism.pojo.User;
import cn.csdj.sism.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * User控制层
 * @author 赖可屹
 * 2023/4/11
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping
    public void insert(@RequestBody User user){
        userService.insert(user);
    }
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Integer id){
        userService.deleteById(id);
    }
    @PutMapping
    public void update(@RequestBody User user){
        userService.update(user);
    }
    @GetMapping("/{id}")
    public User selectById(@PathVariable Integer id){
        return userService.selectById(id);
    }
    @GetMapping
    public List<User> selectAll(){
        return userService.selectAll();
    }
//短信验证（剩余2条）:https://api.smsbao.com/sms?u=tsw2002&p=c4fa040e6b6047da9c2bf7e8d3f2630d&m=13875273310&c=【大计信息】您的验证码是8468,30秒内有效
    @PostMapping("/login")
    public HashMap userLogin(@RequestBody User user,HttpSession session){
        HashMap result = userService.userLogin(user);
        System.out.println(result);
        session.setAttribute("success",result.get("success"));
        return result;
    }
    @GetMapping("/logout")
    public void userLogout(HttpSession session){
        session.removeAttribute("success");
    }
}
